package br.com.felix.clearcase.command;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import br.com.felix.easyclearcase.EasyClearCaseFile;
import br.com.felix.easyclearcase.EasyClearCaseFile.ClearCaseStatus;

/**
 * This class is used to allow users to create their own commands. 
 * */
public class ParametrizedCommand extends ClearToolCommand implements Serializable{

	private static final long serialVersionUID = 6926656356623976970L;

	/**
	 * The cleartool command with the parameters syntax.
	 * */
	private String command;
	
	/**
	 * Maps the parameters with its values.
	 * */
	private Map<String,String>parameters = new HashMap<String,String>();
	
	/**
	 * @see CommandOutput
	 * */
	private CommandOutput output;

	/**
	 * @see CommandTarget
	 * */
	private CommandTarget target;
	
	private String name;
	
	
	/**
	 * Instantiates a new Command with a its cleartool command.
	 * @param command is the cleartool command of this command.
	 * */
	public ParametrizedCommand(String command){
		this.command = command;
		Pattern pParams = Pattern.compile("\\$\\{.*?\\}");
		Matcher m = pParams.matcher(command);
		String parameter;
		while(m.find()){
			parameter = m.group();
			parameter = parameter.substring(2,parameter.length()-1);
			parameters.put(parameter, "");
		}
	}
	
	
	/**
	 * Defines the value of a command's parameter.
	 * If the parameter was already defined, its value is overwritten.
	 * */
	public void setParameter(String paramNumber, String value){
		parameters.put(paramNumber, value);
	}
	
	
	
	/**
	 * Creates a cleartool command by substituting the command parameters with its values.
	 * @return an executable cleartool command.
	 * @see ClearToolCommand#toCommandLine()
	 * */
	@Override
	public String toCommandLine() {
		String clearcaseCommand = command;
		for(String i: parameters.keySet()){
			System.out.println("Parameter value = "+parameters.get(i));
			clearcaseCommand = clearcaseCommand.replaceAll("\\$\\{"+i+"\\}", parameters.get(i));
		}
		System.out.println("Parametrized command = "+clearcaseCommand);
		return clearcaseCommand;
	}



	
	public CommandTarget getTarget() {
		return target;
	}



	
	public void setTarget(CommandTarget target) {
		this.target = target;
	}
	
	

	
	public CommandOutput getOutput() {
		return output;
	}


	
	public void setOutput(CommandOutput output) {
		this.output = output;
	}



	
	public String getName() {
		return name;
	}



	
	public void setName(String name) {
		this.name = name;
	}



	/**
	 * Returns all the parameters that are currently defined with this command.
	 * */
	public Set<String> getParameters() {
		return parameters.keySet();
	}



	@Override
	public void setFile(EasyClearCaseFile file) {
		super.setFile(file);
		parameters.put("file", file.getAbsolutePath().replaceAll("\\\\", "/"));
	}


	@Override
	public String toString() {
		// TODO This may alter some untracked behavior. Look out for bugs ;)
		return name;
	}


	@Override
	public String getCommandName() {
		return "Parametrized Command";
	}


	@Override
	public ClearCaseStatus getStatusAfterExecution() {
		return null;
	}
	
	
}
